<?

class XHR_REQUEST	{
	
	function load_view( $view )	{
	
		include dirname ( __FILE__ ) . "/views/$view.php";
		$ctrl		= camelize( "view.$view" );
		
		return new $ctrl();
	}
}

#	LOGIN Task
class XHRlogin extends XHR_REQUEST {

	function __construct() {
		global $Engine;
		
		$Engine->user_authenticate( simplestring( $_POST['user']), $_POST['pass'] );
	}
	
#	Response
	function response() {
		global $Engine;
		
		return $Engine->user_session( 1 ) ? 
			array( "redirect"=> "?fresh" )
		:	array( "error"=> "not authorised" );
	}
}


#	LOGOUT Task
class XHRlogout extends XHR_REQUEST {

	function __construct() {
		global $DB;
		
		unset( $_SESSION['uid'], $DB->uservars );
	}
	
#	Response
	function response() {

		return array( "redirect"=> "?logout", "message"=> "Thank you for using Engine!", "delay"=> 1 );
	}
}

#	JSON Task
class XHRjson extends XHR_REQUEST {

	function __construct() {
	
		include dirname ( __FILE__ ) . "/elements.php";
		$this->vars = (object) $_POST;
	
	}
	
#	Response
	function response() {
		
		$View = $this->load_view( $this->vars->type );
		$response = (object) $View->{ $this->vars->action }( $this->vars );
		
				$response->column = $_POST['column'];
		return 	$response;

	}
}



#	VIEW Task
class XHRview extends XHR_REQUEST {

	function __construct() {
	
		include dirname ( __FILE__ ) . "/elements.php";
	
	}
	
#	Response
	function response() {
		
		$View = $this->load_view( simplestring( $_POST['type'] ));
		$response = (object) $View->{ simplestring( $_POST['action'] )}();
		
				$response->column = $_POST['column'];
		return 	$response;

	}
}



#	DASHBOARD Task
class XHRdashboard extends XHR_REQUEST {

	function __construct() {}
	
#	Response
	function response() {

		return array(
			"sectionclass"	=> "dashboard",
			"views"			=> engine_get( 'json:dashboard' )
		);
	}
}


#	PAGES Task
class XHRpages extends XHR_REQUEST {

	function __construct() {}
	
#	Response
	function response() {

		return array( "message"=> "pages" );
	}
}

?>